package com.bsoft.gol.editor.service.impl;


import com.alibaba.fastjson.JSONObject;

import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.request.QueryOutpatientListRequest;
import com.bsoft.gol.editor.editor.response.PatientInfo;
import com.bsoft.gol.editor.editor.response.QueryOutpatientListResponse;
import hcn.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.bsoft.gol.editor.service.OutpatientDocService;
import com.bsoft.gol.editor.utils.HttpClient;

import java.util.List;

/**
 * 门诊患者对接his接口
 *
 * @author lxy
 * @date 2020/7/18
 */
@Component
public class OutpatientDocServiceImpl implements OutpatientDocService {


    private final static Logger log = LoggerFactory.getLogger(OutpatientDocServiceImpl.class);


    /**
     * 获取门诊患者列表(7/28列表改造-最新)
     *
     * @param request
     * @return
     */
    @Override
    public BaseResponse<QueryOutpatientListResponse> queryOutpatientList(QueryOutpatientListRequest request) {
        log.info("OutpatientDocServiceImpl.queryOutpatientList 入参：{}", JSONObject.toJSON(request));
        BaseResponse<QueryOutpatientListResponse> result = new BaseResponse<>();
        JSONObject jsonParams = JSONObject.parseObject(JSONObject.toJSONString(request));
        String testUrl = "http://interfacedemo.zk.atag.bsoft.com.cn/outpatient/listPatient";
        //JSONObject jsonObject = HttpClient.doPostJSONRequest(Constants.HIS_CLIENT_URL_01 + Constants.OUTPATIENT_LIST_PATIENT, jsonParams);
        JSONObject jsonObject = HttpClient.doPostJSONRequest(testUrl, jsonParams);

        log.info("OutpatientDocServiceImpl.queryOutpatientList 第三方返回数据:{}", jsonObject.toJSONString(jsonObject));
        try {
            if (jsonObject.getInteger("code") == ResultCode.SUCCESS && jsonObject.containsKey("data") && jsonObject.get("data") != null) {
                QueryOutpatientListResponse response = JSONObject.parseObject(jsonObject.get("data").toString(), QueryOutpatientListResponse.class);
                List<PatientInfo> patientInfoList = response.getPatientList();
                if (patientInfoList != null && patientInfoList.size() > 0) {
                    for (PatientInfo patientInfo : patientInfoList) {
                        String patientId = patientInfo.getPatientId();
//                        if (CommonUtils.isNotEmpty(patientId)) {
//                            List<FusTeamChronicGroupRecordEntity> fusList = fusTeamChronicGroupRecordDAO.findFusRecordsByPersonId(patientId);
//                            if (fusList != null && fusList.size() > 0) {
//                                if (fusList.size() == 1) {
//                                    patientInfo.setChronicNameCollection(fusList.get(0).getChronicDiseaseName());
//                                } else if (fusList.size() > 1) {
//                                    String chronicNameCollection = "";
//                                    for (FusTeamChronicGroupRecordEntity fusBean : fusList) {
//                                        chronicNameCollection += fusBean.getChronicDiseaseName() + ",";
//                                    }
//                                    chronicNameCollection = chronicNameCollection.substring(0, chronicNameCollection.length() - 1);
//                                    patientInfo.setChronicNameCollection(chronicNameCollection);
//                                }
//                            }
//
//                        }
                    }
                }
                result.setData(response);
                log.info("OutpatientDocServiceImpl.queryOutpatientList 出参：{}", jsonObject.toJSONString(result));
            } else {
                result.setCode(jsonObject.getInteger("code"));
                result.setMessage(jsonObject.getString("message"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("HIS返回数据JSON解析异常"+ e.getMessage());
            result.setCode(ResultCode.ERROR);
        }
        return result;
    }

}
